package des.wangku.operate.standard.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import des.wangku.operate.standard.desktop.DesktopConst;
import des.wangku.operate.standard.subengineering.textContent.TextReplace;
import des.wangku.operate.standard.utls.UtilsDialogState;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class TextCotentDialog extends Dialog {

	Shell parent;
	private Shell shell_1=null;
	TabFolder tabFolder=null;
	TabItem tabItem_1=null;
	Group group=null;
	private Text text;

	/**
	 * 构造函数
	 * @param parent Shell
	 * @param style int
	 */
	public TextCotentDialog(Shell parent, int style) {
		super(parent, style);
		init(parent, style);
	}

	/**
	 * 构造函数 内容使用字符串进行显示
	 * @param parent Shell
	 * @param style int
	 * @param content String
	 */
	public TextCotentDialog(Shell parent, int style, String content) {
		super(parent, style);
		init(parent, style);
		if (content != null) text.setText(content);
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		UtilsDialogState.changeDialogCenter(parent, shell_1);
	}



	/**
	 * 初始化
	 * @param parent Shell
	 * @param style int
	 */
	private final void init(Shell parent, int style) {
		structure(parent, style);
		initialization();
	}

	/**
	 * 初始化
	 */
	void initialization() {
		shell_1.setText(DesktopConst.ACC_AuthorTitle + "-修改文本内容");
		shell_1.setSize(528,433);
		shell_1.addDisposeListener(new DisposeListener() {
			@Override
			public void widgetDisposed(DisposeEvent e) {
				logger.warn("缷载弹出窗口");
			}

		});
		//shell.setImage(DesktopConst.ACC_Shell);

		tabFolder = new TabFolder(shell_1, SWT.NONE);
		tabFolder.setBounds(10, 10, 500, 385);
		
		tabItem_1 = new TabItem(tabFolder, SWT.NONE);
		tabItem_1.setText("文本修改");
		group = new Group(tabFolder, SWT.NONE);
		tabItem_1.setControl(group);
		group.setBounds(15, 25, 470, 345);
		text = new Text(group, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		text.setBounds(10, 24, 450, 200);
		
		button = new Button(group, SWT.NONE);
		button.setBounds(10, 241, 124, 23);
		button.setText("从配置文件中替换");
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog fileDialog=new FileDialog(shell_1,SWT.OPEN|SWT.MULTI);
				fileDialog.setFilterExtensions(new String[]{"*.properties"});
				fileDialog.open();
				String [] playslist=fileDialog.getFileNames();//这里是获取文件文件的名字 字符数组
				String catalogue=fileDialog.getFilterPath();//这里是获取文件夹的名字这个不是字符数组   但是通常配合上面的得到文件的全路径
				System.out.println(catalogue);
				String content=text.getText();
				for(String s:playslist){
					String pathfile=catalogue+"/"+s;
					content=TextReplace.replaceFromProperties(content, pathfile);
				}
				text.setText(content);
			}
		});
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell_1.open();
		shell_1.layout();
		Display display = getParent().getDisplay();
		while (!shell_1.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return null;
	}

	/**
	 * 前期构造函数
	 * @param parent Shell
	 * @param style int
	 */
	public void structure(Shell parent, int style) {
		this.parent = parent;
		shell_1 = new Shell(parent);
	}

	/** 日志 */
	static final Logger logger = LoggerFactory.getLogger(TextCotentDialog.class);
	private Button button;
}
